package ozu.project;

import ozu.project.State.FortifyState;
import ozu.project.State.ReinforceState;
import ozu.project.State.AttackState;
import net.miginfocom.swing.MigLayout;
import ozu.project.continent.OzuContinent;
import ozu.project.continent.ContinentInfoComponent;

import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.io.File;


public class RiskInformationComponent extends JComponent {
    private RiskTurkey risk;

    
    

	private JButton nextPlayerButton = new JButton(new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            reinforceButton.setEnabled(true);
            setActiveButton(reinforceButton);
            attackButton.setEnabled(true);
            risk.nextPlayer();
           
        }
    });

    private ActionButton reinforceButton = new ActionButton(true, new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            setActiveButton(reinforceButton);
            risk.setAbstractState(new ReinforceState(risk));

        }
    });

    private ActionButton attackButton = new ActionButton(false, new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            setActiveButton(attackButton);
            reinforceButton.setEnabled(false);
            risk.setAbstractState(new AttackState(risk));
        }
    });

    private ActionButton fortifyButton = new ActionButton(false, new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            setActiveButton(fortifyButton);
            reinforceButton.setEnabled(false);
            attackButton.setEnabled(false);
            risk.setAbstractState(new FortifyState(risk));
            risk.resetFocus();
        }
    });

    public RiskInformationComponent(RiskTurkey risk) {
        this.setLayout(new MigLayout());
        this.risk = risk;
        this.addPlayersInfo();
        this.addStateButtons();
        this.addContinentsInfo();
       
    }

    private void addPlayersInfo() {
        for (OzuPlayer player : risk.getPlayers()) {
            this.add(new PlayerComponent(player));
        }
    }

    private void addStateButtons(){
        reinforceButton.setText("Reinforce");
        this.add(reinforceButton);

        attackButton.setText("Attack");
        this.add(attackButton);

        fortifyButton.setText("Fortify");
        this.add(fortifyButton);

        nextPlayerButton.setText("Next ozuPlayer");
        this.add(nextPlayerButton, "wrap");
    }

    private void addContinentsInfo() {
        for (OzuContinent continent : risk.getContinents()) {
            this.add(new ContinentInfoComponent(continent));
        }
    }

    public void setActiveButton(ActionButton actionButton){
        attackButton.setActive(false);
        reinforceButton.setActive(false);
        fortifyButton.setActive(false);
        actionButton.setActive(true);
    }
}
